package com.rock.drawboard.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.rock.drawboard.MyApplication;
import com.rock.drawboard.R;
import com.rock.drawboard.ui.view.ClearEditText;
import com.rock.drawboard.utils.NetworkUtil;
import com.rock.drawboard.utils.SharedPrefUtil;
import com.rock.drawboard.utils.ToastUtil;

import java.net.SocketException;

/**
 * Created by 昌宜 on 2017/3/24.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private ClearEditText mLoginEdt, password;
    private Button mLoginBtn;
    private SharedPrefUtil mPrefGlobal;
    private TextView author;

    private static final long CLICK_INTERVAL_TIME = 500;
    private static long lastClickTime = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //初始化 SharedPrefUtil
        mPrefGlobal = new SharedPrefUtil.Builder().setFileName(SharedPrefUtil.FILE_USER).create(this);
        initView();
    }

    private void initView() {
        mLoginEdt = ((ClearEditText) findViewById(R.id.login_user_edt));
        password = ((ClearEditText) findViewById(R.id.password));
        mLoginBtn = ((Button) findViewById(R.id.client_login));
        author = ((TextView) findViewById(R.id.author));
        mLoginBtn.setOnClickListener(this);

        findViewById(R.id.server_login).setOnClickListener(this);
        author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取系统当前毫秒数，从开机到现在的毫秒数(手机睡眠时间不包括在内)
                long currentTimeMillis = SystemClock.uptimeMillis();
                //两次点击间隔时间小于300ms代表双击
                if (currentTimeMillis - lastClickTime < CLICK_INTERVAL_TIME) {
                    if (mLoginEdt.getVisibility() == View.GONE) {
                        mLoginEdt.setVisibility(View.VISIBLE);
                        password.setVisibility(View.VISIBLE);
                    } else {
                        mLoginEdt.setVisibility(View.GONE);
                        password.setVisibility(View.GONE);
                    }
                    return;
                }
                lastClickTime = currentTimeMillis;
            }
        });
        try {
            String ipAddress = NetworkUtil.getInstance().getIPAddress(this);
            TextView ip = (TextView)findViewById(R.id.ip_text);
            ip.setText(ipAddress);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        mLoginEdt.setText(mPrefGlobal.get(SharedPrefUtil.server_ip, ""));
        password.setText(mPrefGlobal.get(SharedPrefUtil.password, ""));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.server_login:
                serverLogin();
                break;
            case R.id.client_login:
                String ip = mLoginEdt.getText().toString();
                String passW = password.getText().toString();
                if(TextUtils.isEmpty(ip)) {
                    ToastUtil.makeToastShort("ip address cannot be empty");
                    return;
                }
                if(TextUtils.isEmpty(passW)) {
                    ToastUtil.makeToastShort("password cannot be empty");
                    return;
                }
                if(!"20222023".equals(passW)) {
                    ToastUtil.makeToastShort("sorry, password is error");
                    return;
                }
                clientLogin();
                break;
        }
    }

    //客户端 - 游客模式登录
    private void clientLogin() {
        //记录连接成功的ip
        mPrefGlobal.put(SharedPrefUtil.server_ip, mLoginEdt.getText().toString());
        mPrefGlobal.put(SharedPrefUtil.password, password.getText().toString());
        MyApplication.isServer = false;
        MyApplication.server_ip = mLoginEdt.getText().toString();
        startActivity(new Intent(LoginActivity.this,MainActivity.class));
        finish();
    }

    private void serverLogin() {
        MyApplication.isServer = true;
        startActivity(new Intent(LoginActivity.this,MainActivity.class));
        finish();
    }

}
